package com.wanqing.community.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateTools {

	/**
	 * 得到当前系统时间 以字符串形式返回
	 * @return String
	 */
	public static String getCurrentDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	public static String getCurrentSysDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(new Date());
	}
	
	public static String getCurrentSysTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(new Date());
	}
	
	/**
	 * 得到当前系统日期
	 * @param format 日期格式
	 * @return
	 */
	public static String getCurrentSysDate(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}
	
	/**
	 * 得到当前系统日期
	 * @param format 日期格式
	 * @return
	 */
	public static String getCurrentSysDate(String format,int day){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, day);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param date
	 * @param format 日期格式
	 * @return
	 */
	public static String dateToStr(Date date,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	/**
	 * 得到当前系统时间 以字符串形式返回
	 * @return String
	 */
	public static String getCurTime(){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat simpleDateTimeFormat  =   new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

		  //SimpleDateFormat simpleDateTimeFormat  =   new  SimpleDateFormat( "yyyy-MM-dd" );
		  c  =  Calendar.getInstance(Locale.CHINESE);
		return simpleDateTimeFormat.format(c.getTime());
	}

	/**
	 * Date()转换成String(格式yyyy-MM-dd HH:MM:ss)
	 * @return String
	 */
	public static String dateToString(Date date){
		if(date == null){
			return "";
		}
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	/**
	 * java.sql.DATE格式化
	 * @param date
	 * @return
	 */
	public static String dateToString(java.sql.Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}


	/**
	 * Date()转换成String
	 * @return String
	 */
	public static String dateToString2(Date date){
		DateFormat df = new SimpleDateFormat("yyyyMMddHHMMssssss");
		return df.format(date);
	}

	public static String dateToString(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(new Date());
	}


	/**
	 * Date()转换成String（格式 yyyy-MM-dd）
	 * @return String
	 */
	public static String dateToString3(Date date){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	/**
	 * 验证目标字符是否符合时间格式
	 * 1、如果符合返回true， 否则返false
	 * @param targetTime
	 * @return boolean
	 */
	public static boolean checkTimes(String targetTime){
		if(null==targetTime || "".equals(targetTime.trim())){
			return false;
		}else{
			Pattern p = Pattern.compile("^(([0-1][0-9])|([2][0-4])):[0-5][0-9]:[0-5][0-9]$");
			Matcher m = p.matcher(targetTime);
			return m.matches();
		}
	}
	
	public static boolean checkDateTimes(String sourceDate){
		boolean result = true;
		if(StringTools.isEmptyOrNull(sourceDate)){
			result = false;
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				sdf.parse(sourceDate.trim());
			} catch (ParseException e) {
				result = false;
			}
		}
		return result;
	}
	
	/**
	 * 检查日期格式是否合法(yyyy-MM-dd)
	 * @param sourceDate日期字符串
	 * @return合法返回true,否则返回false
	 */
	public static boolean checkDate(String sourceDate){
		boolean result = true;
		if(StringTools.isEmptyOrNull(sourceDate)){
			result = false;
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				sdf.parse(sourceDate.trim());
			} catch (ParseException e) {
				result = false;
			}
		}
		return result;
	}

	/**
	 * 字符串转为日期
	 * @param source
	 * @return Date
	 */
	public static java.sql.Date stringToDate(String source){
		java.sql.Date date = null;
		try{
			date = java.sql.Date.valueOf(source.trim());
		}catch (IllegalArgumentException e) {
			date=null;
		}
		return date;
	}

	/**
	 * 字符串转为时间
	 * @param source
	 * @return
	 */
	public static Date stringToDates(String source){
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		if(StringTools.isEmptyOrNull(source)){
			return null;
		}else{
			try {
				return sdf.parse(source);
			} catch (ParseException e) {
				return null;
			}
		}
	}
	
	public static Date stringToDates(String source,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if(StringTools.isEmptyOrNull(source)){
			return null;
		}else{
			try {
				return sdf.parse(source);
			} catch (ParseException e) {
				return null;
			}
		}
	}

	/**
	 * 字符串转日期
	 * @param source
	 * @return
	 */
	public static Date stringToDate2(String source){		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(StringTools.isEmptyOrNull(source)){
			return null;
		}else{
			try {
				return sdf.parse(source);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}
	}
	
	public static String strToDate(String source){
		if(StringTools.isEmptyOrNull(source)){
			return getCurrentDate();
		}else{
			Date date = stringToDate2(source);
			if(null==date){
				return getCurrentDate();
			}else{
				return dateToString3(date);
			}
		}
	}

	/**
	 * 判断两个时间格式的字符串的大小
	 * 如果time1在time2前则返回true否则返回false
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static boolean checkTwoTime(String time1,String time2){
		Date d1 = stringToDates(time1);
		Date d2 = stringToDates(time2);
		return d1.before(d2);
	}
	
	public static String stringToDate(String source,String informat,String outformat){
		Date date = stringToDates(source, informat);
		if(null==date){
			return "";
		}
		return dateToStr(date, outformat);
	}



	/**
	 * 比较两个时间的大小
	 * 1,如果time1==time2返回0
	 * 2,如果time1<time2 返回小于0
	 * 3,如果time1>time2返回大于0
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static int decideTwoTime(String time1,String time2){
		Date t1 = stringToDates(time1,"HH:mm");
		Date t2 = stringToDates(time2,"HH:mm");
		return t1.compareTo(t2);
	}

	/**
	 * 格式化数字保留两位小数（四舍五入）
	 * @param inpara
	 * @return string
	 */
	public static String formartNumber(double inpara){
		DecimalFormat   df   =   new   DecimalFormat("###,##0.00");
		return (df.format(inpara));
	}


	/**
	 * 根据日子获得几天前或后
	 * @param date 某个时间
	 * @param day 前几天（带“-”，如-3）或后几天
	 * @return
	 */
	public static String getdateForDayToString(Date date,int day){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		// -1表示前一天，+1表示后一天，依次类推
		long myTime = (date.getTime() / 1000) + (day) * 60 * 60 * 24;
		date.setTime(myTime * 1000);
		String strDay = df.format(date);

		return strDay;
	}
	
	/**
	 * 根据日子获得几天前或后
	 * @param date 某个时间
	 * @param day 前几天（带“-”，如-3）或后几天
	 * @return
	 */
	public static String getdateForDayToString(Date date,int day,String format){
		DateFormat df = new SimpleDateFormat(format);

		// -1表示前一天，+1表示后一天，依次类推
		long myTime = (date.getTime() / 1000) + (day) * 60 * 60 * 24;
		date.setTime(myTime * 1000);
		String strDay = df.format(date);

		return strDay;
	}

	public static Date string2Datetime(String source){
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return df.parse(source);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date string2Date(String source){
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
		try {
			return df.parse(source);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * <p>Description:日期sourceDate增加或减少months月<p>
	 * <p>modifiContent:<p>
	 * @param sourceDate
	 * @param format
	 * @param months
	 * @return
	 */
	public static String getdateForMonth(String sourceDate,String format,int months){
		if(StringTools.isEmptyOrNull(sourceDate)){
			return "";
		}
		Date date = stringToDates(sourceDate,format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, months);
		Date result = calendar.getTime();
		return dateToStr(result, format);
	}
	
	/**
	 * <p>Description:日期sourceDate增加或减少days天,如果days为负数则是减少否则为增加<p>
	 * <p>modifiContent:<p>
	 * @param sourceDate
	 * @param format
	 * @param days
	 * @return
	 */
	public static String getdateForDayToString(String sourceDate,String format,int days){
		if(StringTools.isEmptyOrNull(sourceDate)){
			return "";
		}
		Date date = stringToDates(sourceDate,format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, days);
		Date result = calendar.getTime();
		return dateToStr(result, format);
	}
	
	/**
	 * <p>Description:日期sourceDate增加或减少days天,如果days为负数则是减少否则为增加<p>
	 * <p>modifiContent:<p>
	 * @param sourceDate
	 * @param format
	 * @param days
	 * @return
	 */
	public static String getdateForDayToString(String sourceDate,String informat,String outformat,int days){
		if(StringTools.isEmptyOrNull(sourceDate)){
			return "";
		}
		Date date = stringToDates(sourceDate,informat);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, days);
		Date result = calendar.getTime();
		return dateToStr(result, outformat);
	}
	
	/**
	 * <p>Description:日期sourceDate增加或减少hours小时,如果hours为负数则是减少否则为增加<p>
	 * <p>modifiContent:<p>
	 * @param sourceDate
	 * @param format
	 * @param days
	 * @return
	 */
	public static String getdateForHourToString(String sourceDate,String format,int hours){
		if(StringTools.isEmptyOrNull(sourceDate)){
			return "";
		}
		Date date = stringToDates(sourceDate,format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, hours);
		Date result = calendar.getTime();
		return dateToStr(result, format);
	}
	
	/**
	 * <p>Description:得到系统当天的星期<p>
	 * <p>modifiContent:<p>
	 * @return
	 */
	public static String getWeek(int day){
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, day);
		int index = calendar.get(Calendar.DAY_OF_WEEK)-1;
		if(index<0){
			index=0;
		}
		return weekDays[index];
	}
	
	

	
	
	/**
	 * <p>Description:计算年，从2013年到当前年并且往后追加两年的所有年份<p>
	 * @modifier 
	 * @modifiDate 
	 * <p>modifiContent:<p>
	 * @return List<String>
	 */
	public static List<String> calculationYear(){
		//String temp = "2005,2006,2007,2008,2009,2010,";
		List<String> years = new ArrayList<String>();
		for(int i = 0;i<5;i++){
			int yr = 2013+i;
			years.add(String.valueOf(yr));
		}
		Calendar clen = Calendar.getInstance();
		clen.add(Calendar.YEAR, -1);
		String preYear = String.valueOf(clen.get(Calendar.YEAR));		
		if(!years.contains(preYear)){
			years.add(preYear);
		}
		clen.add(Calendar.YEAR, 1);
		String currYear = String.valueOf(clen.get(Calendar.YEAR));
		if(!years.contains(currYear)){
			years.add(currYear);
		}
		clen.add(Calendar.YEAR, 1);
		String nextYear = String.valueOf(clen.get(Calendar.YEAR));
		if(!years.contains(nextYear)){
			years.add(nextYear);
		}
		clen.add(Calendar.YEAR, 1);
		String lastYear = String.valueOf(clen.get(Calendar.YEAR));
		if(!years.contains(lastYear)){
			years.add(lastYear);
		}
		return years;
	}	
}
